package two;

import java.util.Scanner;

public class demo4 {
    private static final String[] DIGITS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] UNITS = {"", "十", "百", "千", "万", "亿"};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个9位以内的整数：");
        long num = scanner.nextLong();
        String chinese = toChinese(num);
        System.out.println(chinese);
    }

    private static String toChinese(long num) {
        if (num == 0) {
            return DIGITS[0];
        }
        StringBuilder sb = new StringBuilder();
        if (num < 0) {
            sb.append("负");
            num = -num;
        }
        int unitIndex = 0;
        boolean fourJump = false;
        int fourIndex = 0;
        while (num > 0) {
            int kk = (int) (num%10);
            if (unitIndex%4 == 0 && unitIndex > 0) {
                fourIndex++;
                fourJump = true;
            }
            if (unitIndex%4 == 1) {
                unitIndex = 1;
            }
            if (kk == 0) {
                if (unitIndex != 0) {
                    sb.insert(0, DIGITS[kk]);
                }
            }else{
                if (fourJump) {
                    sb.insert(0, UNITS[4+fourIndex-1]);
                    sb.insert(0, DIGITS[kk]);
                    fourJump = false;
                }else{
                    sb.insert(0, UNITS[unitIndex]);
                    sb.insert(0, DIGITS[kk]);
                }
            }
            num /= 10;
            unitIndex++;
        }
        return sb.toString();
    }
}